<?php
/**
 * Created by PhpStorm.
 * User: 伟鑫
 * Date: 2019/1/8
 * Time: 16:58
 */

namespace app\admin\controller;


use app\admin\model\CouponModel;

class Coupon extends Base
{
    // 优惠券列表
    public function index()
    {
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $where = [];
            if (!empty($param['searchText'])) {
                $where['title'] = ['like', '%' . $param['searchText'] . '%'];
            }
            $article = new CouponModel();
            $selectResult = $article->getcouponList($where, $offset, $limit);

            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }

            $return['total'] = $article->getAllArticles($where);  // 总数据
            $return['rows'] = $selectResult;

            return json($return);
        }
        return $this->fetch();
    }

    // 添加优惠券
    public function add()
    {
        if(request()->isPost()){
            $param = input('post.');
            list($atime,$btime)=explode('~',$param['ftime']);
            $param['atime']= strtotime($atime);
            $param['btime'] = strtotime($btime);
            $param['addtime'] = time();
            unset($param['ftime']);
            $article = new CouponModel();
            $flag = $article->add($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }

        return $this->fetch();
    }

    public function edit()
    {
        $article = new CouponModel();
        if(request()->isPost()){

            $param = input('post.');
            list($atime,$btime)=explode('~',$param['ftime']);
            unset($param['ftime']);
            $param['atime']= strtotime($atime);
            $param['btime'] = strtotime($btime);
            $flag = $article->edit($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }

        $id = input('param.id');
        $this->assign([
            'coupon' => $article::get($id)
        ]);
        return $this->fetch();
    }

    public function del()
    {
        $id = input('param.id');

        $article = new CouponModel();
        $flag = $article->del($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'coupon/edit',
                'href' => url('coupon/edit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'coupon/del',
                'href' => "javascript:del(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }

}